import DotPersonnelAddTpl from './news.html';
import _ from 'underscore';
import Util from '../../../../common-component/util/util.js';
import upload from '../../../../common-component/upload/upload';
import Table from '../../../../common-component/table/table.js';
import ServiceCommon from '../../../service-common/service-common.js';
import API from '../../../../api/Api.js';

import "../news.scss";

export default function DotPersonnelAdd() {
    var category = [],news = [];
    //获取新闻分类
    $.ajax({
        url: API.categorygGetAll,
        type: 'get',
        async:false,
        success(data) {
            if(data.success){
                category = data.data;
            }else{
                Util.alertMessage(data.error)
            }
        }
    });

    //查询新闻信息
    var newsId = Util.GetQueryString('id');
    $.ajax({
        url: API.findNewsById,
        type: 'get',
        data:{id:newsId},
        async:false,
        success(data) {
            if(data.success){
                news = data.data;
            }else{
                Util.alertMessage(data.error)
            }
        }
    });

    $("#app-container").html( DotPersonnelAddTpl({category:category,news:news}) );

    //实例化编辑器
    UM.delEditor('myEditor');
    var ue = UM.getEditor('myEditor');
    ue.setContent(news.info);

    const licenseImgHeader = upload("#upload-img-wrap",{itemsWrap: '#js-item-wrap',memory: '#js-memory'}), v = $('#update_dot').easyform();
    var _this = this,params = {};

    v.success = function (ef){//验证成功
        params = $(ef.form).serializeObject(params);
        params.id = newsId;
        params.info = params.editorValue;

        params.stick = 0;
        params.shelf = 0;
        params.recommend = 0;
        if(params.levelType == 1){//上架
            params.shelf = 1;
        }else if(params.levelType == 2){//置顶
            params.stick = 1;
        }else{//推荐
            params.recommend = 1;
        }
        delete params.editorValue;
        delete params.levelType;
        $.ajax({
            url: API.newsUpdate,
            type: 'post',
            data:params,
            success(data) {
                if(data.success){
                    Util.alertMessage('发布新闻成功！',function(){
                        window.location.href = '#/news';
                    });
                }else{
                    Util.alertMessage(data.message);
                }
            }
        });
    }

    const handlers = {
        params:{},
        init: function(evt) {
            const _this = this;
            this.bindEvent();
        },
        /**
         * 绑定事件
         */
        bindEvent: function() {
            const _this = this;
            $(".js-handle").on('click', function(e) {
                const $this = $(this), handle = $this.data('handle');
                _this[handle] && _this[handle]($this, e);
            })
            return this;
        },
        /**
         * 封面
         */
        licenseImg: function() {
            licenseImgHeader.setOptions({
                maxNumber:1,
                itemsWrap: '#js-item-wrap',
                memory: '#js-memory',
                hasFile: false,
            }).open();
        },
        /**
         * 保存
         */
        submitBtn: function() {

        },
        /**
         * 审核
         */
        reviewBtn: function() {

        },
        /**
         * 返回
         */
        return: function() {
            window.location.href = '#/news'
        }
    }

    handlers.init();
}